﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiApp.Model;
using HitachiLib.Data;

namespace HitachiApp.View
{
    public partial class ScheduleUnitTimeControl : UserControl
    {
        public ScheduleUnitTimeControl()
        {
            InitializeComponent();
        }

        private void DataGridDragDropTarget_ItemDroppedOnTarget(object sender, ItemDragEventArgs e)
        {
            ScheduleUnitTimeControlModel model = DataContext as ScheduleUnitTimeControlModel;
            IList<ViewItem> dataSource = grid.ItemsSource as IList<ViewItem>;
            if (model == null)
                return;
            DragDropControl ddc = DragDropControl.GetInstance();
            ddc.Source = model;
            ddc.src_control = grid;
            ddc.ProcessDDC();
            e.Effects = Microsoft.Windows.DragDropEffects.Copy;
            e.Handled = true;
/*
            SelectionCollection selectionCollection = e.Data as SelectionCollection;
            if (selectionCollection != null)
            {
                IEnumerable<ViewItem> projects = selectionCollection.Select(selection => selection.Item).OfType<ViewItem>();
                if (projects.Any())
                {
                    foreach (ViewItem vi in projects)
                    {
                        Project p = vi.Project;
                        if (!model.Common && p.PlanSchedule.ContainsKey(Schedule.WholeYear))
                        {
                            Schedule s = p.PlanSchedule[Schedule.WholeYear];
                            p.PlanSchedule.Remove(Schedule.WholeYear);
                        }
                        if (!model.ShouldContains(vi))
                            dataSource.Remove(vi);
                    }
                    e.Effects = Microsoft.Windows.DragDropEffects.Copy;
                    e.Handled = true;
                }
            }
*/
        }

        private void DataGridDragDropTarget_Drop(object sender, Microsoft.Windows.DragEventArgs e)
        {
            // Only handle this event if it's a copy.  If the event is not handled
            // the DDTs base implementation of Drop will execute.
            if ((e.AllowedEffects & Microsoft.Windows.DragDropEffects.Copy) == Microsoft.Windows.DragDropEffects.Copy)
            {
                ScheduleUnitTimeControlModel model = DataContext as ScheduleUnitTimeControlModel;
                if (model == null)
                    return;
                IList<ViewItem> dataSource = grid.ItemsSource as IList<ViewItem>;
                DragDropControl ddc = DragDropControl.GetInstance();
                ddc.Target = model;
                ddc.dst_control = grid;
                e.Effects = Microsoft.Windows.DragDropEffects.Copy;
/*

                // Retrieve the dropped data in the first available format.
                object data = e.Data.GetData(e.Data.GetFormats()[0]);

                // The data is the ItemDragEventArgs that was created by the DDT when
                // the drag started.  It contains a SelectionCollection.
                // SelectionCollection's are used by DDTs because they can transfer 
                // multiple objects.  The fact that they store the indexes of the 
                // objects within the source collection also makes reordering items
                // within a source possible.
                ItemDragEventArgs dragEventArgs = data as ItemDragEventArgs;
                SelectionCollection selectionCollection = dragEventArgs.Data as SelectionCollection;
                if (selectionCollection != null)
                {
                    IEnumerable<ViewItem> projects = selectionCollection.Select(selection => selection.Item).OfType<ViewItem>();
                    if (projects.Any())
                    {
                        foreach (ViewItem vi in projects)
                        {
                            Project p = vi.Project;
                            // Invoke our custom Clone method to make a copy.
                            model.ApplyToProject(vi);
                            if (!dataSource.Contains(vi))
                                dataSource.Add(vi);
                        }
                        e.Effects = Microsoft.Windows.DragDropEffects.Copy;
                        e.Handled = true;
                    }
                }
*/
            }
        }
    }
}
